//DC

#pragma semicolon 1 // Force strict semicolon mode.

#include <sourcemod>
#include <sdktools>
#include <tf2_stocks>
#include <tf2items>
#include <psycore>

#define PLUGIN_NAME              "tLoot - Weapons"
#define PLUGIN_AUTHOR            "Thrawn"
#define PLUGIN_VERSION           "1.0.0.0"
#define PLUGIN_CONTACT           "http://aaa.einfachonline.net/"

new Handle:g_hBigRL;
new Handle:g_hBigScat;
new Handle:g_hBigPipe;
new Handle:g_hFastWrench;
new Handle:g_hQuickUber;
new Handle:g_hQuickMini;
new Handle:g_hSlowFlame;
new Handle:g_hSlowKnife;
new Handle:g_hSniperRifleFastCharge;

new Handle:GameConf;
new Handle:hWeaponEquip;

new g_iEffectNum[8] = {-1,...};

new bool:g_bHasUpgrade[MAXPLAYERS + 1] = {false, ...};
new bool:g_bHasUpgradeEquipped[MAXPLAYERS + 1] = {false, ...};

new Handle:g_clientEffectTimer[MAXPLAYERS + 1];

public Plugin:myinfo = {
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_NAME,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_CONTACT
};

/* OnPluginStart()
**
** When the plugin is loaded.
** -------------------------------------------------------------------------- */
public OnPluginStart()
{   
	defineWeapons();
	GameConf = LoadGameConfigFile("givenameditem.games");
	
	StartPrepSDKCall(SDKCall_Player);
	PrepSDKCall_SetFromConf(GameConf, SDKConf_Virtual, "WeaponEquip");
	PrepSDKCall_AddParameter(SDKType_CBaseEntity, SDKPass_Pointer);

	hWeaponEquip = EndPrepSDKCall();
	
	HookEvent("player_death", Event_PlayerDeath);
}

public OnAllPluginsLoaded() {
	if(LibraryExists("psycore")) {
		g_iEffectNum[0] = psyRTD_RegisterEffect(psyRTDEffectType_Good,"Bazinga!","Your weapon has been upgraded!",GiveWeaponUpgrade);
	}		
}

public psyRTDAction:GiveWeaponUpgrade(client) {
	g_bHasUpgrade[client] = true;
	new TFClassType:class = TF2_GetPlayerClass(client);

	switch(class) {
		case TFClass_Scout: {
			ReplaceWeapon(client, 0, g_hBigScat);
		}		
		
		case TFClass_Soldier: {
			ReplaceWeapon(client, 0, g_hBigRL);
		}		

		case TFClass_Pyro: {
			ReplaceWeapon(client, 0, g_hSlowFlame);
		}		
		
		case TFClass_DemoMan: {
			ReplaceWeapon(client, 0, g_hBigPipe);
		}		
		
		case TFClass_Heavy: {
			ReplaceWeapon(client, 0, g_hQuickMini);
		}	

		case TFClass_Engineer: {
			ReplaceWeapon(client, 2, g_hFastWrench);
		}		
		
		case TFClass_Medic: {
			ReplaceWeapon(client, 1, g_hQuickUber);
		}		
		
		case TFClass_Sniper: {
			ReplaceWeapon(client, 0, g_hSniperRifleFastCharge);
		}				

		case TFClass_Spy: {
			ReplaceWeapon(client, 2, g_hSlowKnife);
		}						
	}
	
	return psyRTD_Continue;
}

public ReplaceWeapon(client, slot, Handle:weapon) {
	TF2_RemoveWeaponSlot(client, slot);
	
	new entity = TF2Items_GiveNamedItem(client, weapon);
	SDKCall(hWeaponEquip, client, entity);

	SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", entity);	
}

public DisableEffect(client,effectId) {
	for(new i = 0; i < sizeof(g_iEffectNum); i++) {
		if(effectId == g_iEffectNum[i]) {
			if(g_clientEffectTimer[client] != INVALID_HANDLE)
				TriggerTimer(g_clientEffectTimer[client]);
		}
	}

	g_bHasUpgrade[client] = false;	
}

public defineWeapons() {
	g_hBigRL = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hBigRL, "tf_weapon_rocketlauncher");
	TF2Items_SetItemIndex(		g_hBigRL, 18);
	TF2Items_SetLevel(			g_hBigRL, 100);
	TF2Items_SetQuality(		g_hBigRL, 8);
	TF2Items_SetNumAttributes(	g_hBigRL, 1);
	TF2Items_SetAttribute(		g_hBigRL, 0,   4, 1.5);

	g_hBigPipe = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hBigPipe, "tf_weapon_grenadelauncher");
	TF2Items_SetItemIndex(		g_hBigPipe, 19);
	TF2Items_SetLevel(			g_hBigPipe, 100);
	TF2Items_SetQuality(		g_hBigPipe, 8);
	TF2Items_SetNumAttributes(	g_hBigPipe, 1);
	TF2Items_SetAttribute(		g_hBigPipe, 0,   4, 1.5);

	g_hBigScat = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hBigScat, "tf_weapon_scattergun");
	TF2Items_SetItemIndex(		g_hBigScat, 13);
	TF2Items_SetLevel(			g_hBigScat, 100);
	TF2Items_SetQuality(		g_hBigScat, 8);
	TF2Items_SetNumAttributes(	g_hBigScat, 1);
	TF2Items_SetAttribute(		g_hBigScat, 0,   4, 1.5);

	g_hSniperRifleFastCharge = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hSniperRifleFastCharge, "tf_weapon_sniperrifle");
	TF2Items_SetItemIndex(		g_hSniperRifleFastCharge, 14);
	TF2Items_SetLevel(			g_hSniperRifleFastCharge, 100);
	TF2Items_SetQuality(		g_hSniperRifleFastCharge, 8);
	TF2Items_SetNumAttributes(	g_hSniperRifleFastCharge, 1);
	TF2Items_SetAttribute(		g_hSniperRifleFastCharge, 0,   90, 1.5);	

	g_hSlowFlame = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hSlowFlame, "tf_weapon_flamethrower");
	TF2Items_SetItemIndex(		g_hSlowFlame, 21);
	TF2Items_SetLevel(			g_hSlowFlame, 100);
	TF2Items_SetQuality(		g_hSlowFlame, 8);
	TF2Items_SetNumAttributes(	g_hSlowFlame, 1);
	TF2Items_SetAttribute(		g_hSlowFlame, 0,   32, 0.75);		

	g_hSlowKnife = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hSlowKnife, "tf_weapon_knife");
	TF2Items_SetItemIndex(		g_hSlowKnife, 4);
	TF2Items_SetLevel(			g_hSlowKnife, 100);
	TF2Items_SetQuality(		g_hSlowKnife, 8);
	TF2Items_SetNumAttributes(	g_hSlowKnife, 1);
	TF2Items_SetAttribute(		g_hSlowKnife, 0,   32, 0.33);
	
	g_hQuickMini = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hQuickMini, "tf_weapon_minigun");
	TF2Items_SetItemIndex(		g_hQuickMini, 15);
	TF2Items_SetLevel(			g_hQuickMini, 100);
	TF2Items_SetQuality(		g_hQuickMini, 8);
	TF2Items_SetNumAttributes(	g_hQuickMini, 1);
	TF2Items_SetAttribute(		g_hQuickMini, 0,   32, 0.75);	
		
	g_hFastWrench = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hFastWrench, "tf_weapon_wrench");
	TF2Items_SetItemIndex(		g_hFastWrench, 7);	
	TF2Items_SetLevel(			g_hFastWrench, 100);
	TF2Items_SetQuality(		g_hFastWrench, 8);
	TF2Items_SetNumAttributes(	g_hFastWrench, 2);
	TF2Items_SetAttribute(		g_hFastWrench, 0,   92, 2.00);			
	TF2Items_SetAttribute(		g_hFastWrench, 1,   80, 1.50);			
	
	g_hQuickUber = TF2Items_CreateItem(OVERRIDE_CLASSNAME|OVERRIDE_ITEM_DEF|OVERRIDE_ITEM_LEVEL|OVERRIDE_ITEM_QUALITY|OVERRIDE_ATTRIBUTES);
	TF2Items_SetClassname(		g_hQuickUber, "tf_weapon_medigun");
	TF2Items_SetItemIndex(		g_hQuickUber, 29);	
	TF2Items_SetLevel(			g_hQuickUber, 100);
	TF2Items_SetQuality(		g_hQuickUber, 8);
	TF2Items_SetNumAttributes(	g_hQuickUber, 1);
	TF2Items_SetAttribute(		g_hQuickUber, 0,   10, 1.50);			
	
	
}

public Action:Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast) {
	new client = GetClientOfUserId(GetEventInt(event, "userid"));

	g_bHasUpgradeEquipped[client] = false;
}

public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast) {
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));

	if(g_bHasUpgradeEquipped[victim]) {
		if (victim && IsClientInGame(victim) && !IsClientInKickQueue(victim))
			psyRTD_EffectIsNowDisabled(victim, g_iEffectNum[0]);
		
		g_bHasUpgradeEquipped[victim] = false;
	}
}